package com.teradata.swt;


import io.jsonwebtoken.*;
import io.jsonwebtoken.impl.crypto.MacProvider;

import java.security.Key;

public class CwtTest {
    public static void main(String[] args) {
        Key key = MacProvider.generateKey();

        String compactJws = Jwts.builder()
                .setSubject("Joe")
                //.setClaims(new DefaultClaims().setId(IdGenerator.getId()))
                .signWith(SignatureAlgorithm.HS512, key)
                .compact();

        System.out.println(compactJws);

        String compactJwsaa = compactJws + "abc";

        //System.out.println(Jwts.parser().setSigningKey(key).parseClaimsJws(compactJwsaa).getBody().getSubject());
        //assert Jwts.parser().setSigningKey(key).parseClaimsJws(compactJws+"abc").getBody().getSubject().equals("Joe");

        //compactJws = compactJws + "abc";
        try {

            Jws<Claims> claimsJws = Jwts.parser().setSigningKey(key).parseClaimsJws(compactJws);

            System.out.println("trust");

            System.out.println(claimsJws.getBody().getSubject());

            //OK, we can trust this JWT

        } catch (SignatureException e) {
            System.out.println("not trust");
            //don't trust the JWT!
        }

    }
}
